package com.kssoft.ebd.spider.common;

import com.kssoft.ebd.spider.common.config.SpiderConfig;
import com.kssoft.ebd.spider.pool.WebDriverPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class FrameInitService implements ApplicationRunner {

    private Logger logger=  LoggerFactory.getLogger(FrameInitService.class);
    @Autowired
    private SpiderConfig spiderConfig;

    @Autowired
    private WebDriverPool webDriverPool;


    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("springboot 启动后执行的方法");
        System.getProperties().setProperty("webdriver.chrome.driver",
                spiderConfig.getChromeDriverPath());
        initWebPool();
        logger.info("springboot 完成同步启动方法");
    }


    /***
     * 异步初始化，休眠10秒后开始运行
     */
    public void initWebPool(){
        Thread initPool = new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    Thread.sleep(10*1000);
                    webDriverPool.initWebDriver();
                }catch (Exception e){
                    webDriverPool.closeAll();
                    logger.error("初始化失败",e);
                }
            }
        });
        initPool.start();
    }
}


